/**   
* @Title: TaskConfigListener.java 
* @Package cn.com.gome.task.factory 
* @Description: 任务参数变更监听器
* @author chenmin-ds   
* @date 2015年1月20日 下午4:48:09 
* @company cn.com.gome
* @version V1.0   
*/ 


package cn.com.klun.tjy.factory;

import cn.com.klun.tjy.dao.TaskInfoDAO;
import cn.com.klun.tjy.service.Impl.TaskConfigRunImpl;
import cn.com.klun.util.BasisConstants;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/** 
 * @ClassName: TaskConfigListener 
 * @Description: 任务参数变更监听器
 */
@Service
public class TaskConfigListener {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;
	
	@Autowired
	private TaskInfoDAO taskInfoDAO;
	
	@PostConstruct
	public void taskDeadlock() {
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			JobDetail jobDetail =JobBuilder.newJob(TaskConfigRunImpl.class)
					.withIdentity("TaskConfig", BasisConstants.SYSTEM_TASK_GROUP).build();
			// 这个步骤的作用？传递属性
			jobDetail.getJobDataMap().put("taskInfoDAO", taskInfoDAO);
			//表达式调度构建器
			CronScheduleBuilder scheduleBuilder =
					CronScheduleBuilder.cronSchedule(BasisConstants.taskConfigRule);
			//按新的cronExpression表达式构建一个新的trigger
			CronTrigger trigger =TriggerBuilder.newTrigger()
					.withIdentity("TaskConfig", BasisConstants.SYSTEM_TASK_GROUP)
					.withSchedule(scheduleBuilder).build();
			scheduler.scheduleJob(jobDetail, trigger);
		} catch (SchedulerException e) {
			logger.error("定时器的构造异常，实时器名称：TaskConfig,",e);
		}
	}
}
